package com.wgchao.authmanager.application.controller;

import com.wgchao.authmanager.application.filter.AuthMethod;
import com.wgchao.authmanager.application.filter.Authentication;
import com.wgchao.authmanager.application.service.ResourceService;
import com.wgchao.authmanager.application.vo.HandleVo;
import com.wgchao.authmanager.application.vo.MenuVo;
import com.wgchao.authmanager.application.vo.ProjectVo;
import com.wgchao.authmanager.domain.params.*;
import com.wgchao.authmanager.infrastructure.resp.BackResult;
import com.wgchao.authmanager.query.ProjectInfoQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: ResourceController
 * @author: wgchao
 * @createTime: 2020/5/9 10:31 AM
 * @Description: 资源管理
 */
@RestController
@RequestMapping("/aum/front/resource")
@Api(value = "/aum/front/resource", description = "资源管理")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private ProjectInfoQuery projectInfoQuery;

    @PostMapping("/project")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "新建项目资源", notes = "需要身份验证")
    public BackResult createProject(@ApiParam(value = "项目信息", name = "creator", required = true)
                                        @RequestBody ProjectCreator creator){
        resourceService.addProject(creator);
        return BackResult.success();
    }

    @PutMapping("/project/{projectId}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "修改项目资源信息", notes = "需要身份验证")
    public BackResult resiveProjectInfo(@ApiParam(value = "项目资源标识（url参数）", name = "projectId", required = true)
                                       @PathVariable String projectId,
                                       @ApiParam(value = "项目资源修改信息", name = "updator", required = true)
                                       @RequestBody ProjectUpdator updator){
        resourceService.resiveProjectInfo(projectId, updator);
        return BackResult.success();
    }

    @DeleteMapping("/project/{projectId}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "删除项目资源信息", notes = "需要身份验证")
    public BackResult deleteProjectInfo(@ApiParam(value = "项目资源标识（url参数）", name = "projectId", required = true)
                                        @PathVariable String projectId){
        resourceService.removeProject(projectId);
        return BackResult.success();
    }

    @PostMapping("/menu/{projectId}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "为项目添加菜单资源", notes = "需要身份验证")
    public BackResult addMenuToProject(@ApiParam(value = "项目资源标识（url参数）", name = "projectId", required = true)
                                       @PathVariable String projectId,
                                       @ApiParam(value = "菜单资源信息", name = "menuCreator", required = true)
                                       @RequestBody MenuResourceCreator menuCreator){
        resourceService.addMenuToProject(projectId, menuCreator);
        return BackResult.success();
    }

    @PutMapping("/menu/{projectId}/{menuId}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "修改菜单资源信息", notes = "需要身份验证")
    public BackResult resiveMenuInfo(@ApiParam(value = "项目资源标识（url参数）", name = "projectId", required = true)
                                     @PathVariable String projectId,
                                     @ApiParam(value = "菜单资源标识（url参数）", name = "menuId", required = true)
                                     @PathVariable String menuId,
                                     @ApiParam(value = "菜单资源信息", name = "menuUpdator", required = true)
                                     @RequestBody MenuResourceUpdator menuUpdator){
        resourceService.resiveMenuInfo(projectId, menuId, menuUpdator);
        return BackResult.success();
    }

    @DeleteMapping("/menu/{projectId}/{menuId}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "删除菜单资源信息", notes = "需要身份验证")
    public BackResult removeMenu(@ApiParam(value = "项目资源标识（url参数）", name = "projectId", required = true)
                                 @PathVariable String projectId,
                                 @ApiParam(value = "菜单资源标识（url参数）", name = "menuId", required = true)
                                 @PathVariable String menuId){
        resourceService.removeMenu(projectId, menuId);
        return BackResult.success();
    }

    @PostMapping("/handle")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "新建操作资源", notes = "需要身份验证")
    public BackResult addHandle(@ApiParam(value = "操作资源信息", name = "creator", required = true)
                                @RequestBody HandleCreator creator){
        resourceService.addHandle(creator);
        return BackResult.success();
    }

    @PutMapping("/handle/{handleId}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "修改操作资源信息", notes = "需要身份验证")
    public BackResult resiveHandleInfo(@ApiParam(value = "操作资源标识（url参数）", name = "handleId", required = true)
                                       @PathVariable String handleId,
                                       @ApiParam(value = "操作资源信息", name = "updator", required = true)
                                       @RequestBody HandleUpdator updator){
        resourceService.resivehandleInfo(handleId, updator);
        return BackResult.success();
    }

    @DeleteMapping("/handle/{handleId}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "删除操作资源信息", notes = "需要身份验证")
    public BackResult deleteHandleInfo(@ApiParam(value = "操作资源标识（url参数）", name = "handleId", required = true)
                                       @PathVariable String handleId){
        resourceService.removeHandle(handleId);
        return BackResult.success();
    }

    @GetMapping("/project/all/list")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "查询所有的项目资源", notes = "需要身份验证")
    public BackResult<List<ProjectVo>> searchAllProjects(){
        List<ProjectVo> projectVos = projectInfoQuery.searchAllProjects(null);
        return BackResult.success().setData(projectVos);
    }

    @GetMapping("/project/canuse/list")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "查询所有的可用项目资源", notes = "需要身份验证")
    public BackResult<List<ProjectVo>> searchCanUseProjects(){
        List<ProjectVo> projectVos = projectInfoQuery.searchAllProjects(false);
        return BackResult.success().setData(projectVos);
    }

    @GetMapping("/menu/search/{projectId}/list")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "查询项目下的菜单目录", notes = "需要身份验证")
    public BackResult<List<MenuVo>> searchMenusOfProject(@ApiParam(value = "项目资源标识（url参数）", name = "projectId", required = true)
                                                             @PathVariable String projectId){
        List<MenuVo> menuVos = projectInfoQuery.searchMenusOfProject(projectId);
        return BackResult.success().setData(menuVos);
    }

    @GetMapping("/handle/all/list")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "查询所有的操作资源目录", notes = "需要身份验证")
    public BackResult<List<HandleVo>> searchHandles(){
        List<HandleVo> handleVos = projectInfoQuery.searchHandles();
        return BackResult.success().setData(handleVos);
    }

}
